package com.example.student.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.student.entity.SysMenu;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;

/**
 * 菜单Mapper接口
 */
public interface SysMenuMapper extends BaseMapper<SysMenu> {
    
    /**
     * 根据用户ID查询其拥有的菜单权限
     */
    @Select("SELECT DISTINCT m.permission FROM sys_menu m " +
            "JOIN sys_role_menu rm ON m.id = rm.menu_id " +
            "JOIN sys_user_role ur ON rm.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} " +
            "AND m.status = 1 " +
            "AND m.deleted = 0 " +
            "AND m.permission IS NOT NULL " +
            "AND m.permission != ''")
    List<String> selectPermissionsByUserId(@Param("userId") Long userId);
    
    /**
     * 查询所有菜单权限
     */
    @Select("SELECT permission FROM sys_menu " +
            "WHERE status = 1 " +
            "AND deleted = 0 " +
            "AND permission IS NOT NULL " +
            "AND permission != ''")
    List<String> selectAllPermissions();
    
    /**
     * 根据角色ID查询其拥有的菜单ID列表
     */
    @Select("SELECT menu_id FROM sys_role_menu WHERE role_id = #{roleId}")
    List<Long> selectMenuIdsByRoleId(@Param("roleId") Long roleId);
    
    /**
     * 查询所有菜单列表（树形结构）
     */
    @Select("SELECT * FROM sys_menu " +
            "WHERE deleted = 0 " +
            "ORDER BY sort_order ASC")
    List<SysMenu> selectAllMenus();
    
    /**
     * 根据用户ID查询其拥有的菜单列表
     */
    @Select("SELECT DISTINCT m.* FROM sys_menu m " +
            "JOIN sys_role_menu rm ON m.id = rm.menu_id " +
            "JOIN sys_user_role ur ON rm.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} " +
            "AND m.status = 1 " +
            "AND m.deleted = 0 " +
            "ORDER BY m.sort_order ASC")
    List<SysMenu> selectMenusByUserId(@Param("userId") Long userId);
} 